package com.intelligentclass.services;

import com.intelligentclass.entity.Courseware;
import com.intelligentclass.entity.Exercise;
import com.intelligentclass.repositories.CoursewareRepository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CoursewareService {

    public CoursewareRepository coursewareRepository;

    public CoursewareService(CoursewareRepository coursewareRepository) {
        this.coursewareRepository = coursewareRepository;
    }

    public List<Courseware> getAllCoursewaresByCid(int cid) {
        List<Courseware> list = coursewareRepository.findAllByCid(cid);
        if (!list.isEmpty()) {
            System.out.println("empty");
            return list;
        }
        else{
            System.out.println("null");
            return null;
        }
    }

    public Courseware getCoursewareById(int id) {
        Courseware courseware = coursewareRepository.findById(id).orElse(null);
        return courseware;
    }

    public List<Courseware> getAllTopNodes(int cid) {
        List<Courseware> list = coursewareRepository.findAllTopNode(cid);
        if (!list.isEmpty()) {
            System.out.println("empty");
            return list;
        }
        else{
            System.out.println("null");
            return null;
        }
    }

    public List<Courseware> getAllChildNodes(int cid, int parent){
        List<Courseware> list = coursewareRepository.findAllChildCourseware(cid, parent);
        if (!list.isEmpty()) {
            System.out.println("empty");
            return list;
        }
        else{
            System.out.println("null");
            return null;
        }
    }

    public Courseware save(Courseware courseware){
        return coursewareRepository.save(courseware);
    }

    public Courseware getCoursewareByCidAndName(int cid, String name){
        return coursewareRepository.findByCidAndName(cid, name);
    }

    public Courseware getCourseware(int cid, String name, int parent){
        return coursewareRepository.find(cid, name, parent);
    }
}
